import { defineStore } from 'pinia'
import { ref } from 'vue'

// 用户持久层数据函数
export const useUserInfoStore = defineStore('userInfo', () => {
    // 用户数据（账号、id、头像、昵称）
    const info = ref()
    // 用户订单
    const order = ref([])
    // 用户收货地址
    const address = ref()
    // 用户token
    const token = ref(null)
    // 是否记住用户
    const remember = ref(false)

    // setter函数
    const setInfo = (newInfo) => {
        // 存入数据
        info.value = newInfo;
        // 设置12小时过期
        // expireTime.value = Date.now() + 12 * 60 * 60 * 1000;
    }

    // 存入订单
    const setOrder = (newOrder) => {
        order.value = newOrder
    }

    // 存入收货地址
    const setAddress = (newAddress) => {
        address.value = newAddress
    }

    // 存入token
    const setToken = (value) => {
        token.value = value
    }

    // 数据清空函数
    const clearAll = () => {
        info.value = null
        token.value = null
        address.value = null
        order.value = []
    }

    return {info, address, token, remember, order, setInfo, setOrder, setAddress, clearAll, setToken}
}, {persist : true});